<?php

namespace app\admin\model;

use app\common\library\Auth;
use app\common\model\WorkTopic;
use fast\BaiduNlp;
use fast\Random;
use think\Model;

class Work extends Model
{
    // 表名
    protected $name = 'work';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'ctime';
    protected $updateTime = 'utime';
    
    // 追加属性
    protected $append = [
        'status_text',
        'ctime_text',
        'utime_text',
        'category_name',
        'pcategory_name',
        'creator_text'
    ];

    protected static function init(){
        $auth = Auth::instance();
        Work::event('before_insert',function ($work)use ($auth){
           $max_sno=db('work')->max('sno');
           if (empty($max_sno)){$max_sno=10000;}
            $work->sno=$max_sno+1;
           $check_cid=db('category')->where('id',$work->cid)->find();
           if ($check_cid['pid']>0){
               $work->cid=$check_cid['pid'];
               $work->pid=$check_cid['id'];
           }

           $work->creator=$auth->id;
        });
        Work::event('before_update',function ($work)use ($auth){
           $work->updator=$auth->id;
        });
        Work::event('after_insert',function ($work){
           $title=$work->title;
           $part_words=part_words($title);
           foreach ($part_words as $v){
               $data=[
                   'pid'=>$work->id,
                   'words'=>$v['word'],
                   'hot'=>1
               ];
               WorkWords::create($data,true);
               \model('work_words')->where('words',$v['word'])->setInc('hot');
           }
           //自动分类
            WorkTopic::auto_cate($work->title,$work->content);
        });
    }

    public function getCreatorTextAttr($value,$data)
    {
        return (string)db('user')->where('id',$data['creator'])->value('username');
    }

    public function getCategoryNameAttr($value,$data)
    {
        return (string)db('category')->where('id',$data['cid'])->value('name');
    }

    public function getPcategoryNameAttr($value,$data)
    {
        return (string)db('category')->where('id',$data['pid'])->value('name');
    }

    public function getStatusList()
    {
        return ['1' =>'启用','0'=>'禁用'];
    }     


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['ctime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['utime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
